﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Social.Twitter;
using System.Net;
using Social.Facebook;

namespace Logic.BusinessLogic
{
	public class StateUpdater
	{
		public BO.Account Account { get; set; }
		private CookieContainer cook;
		TwitterAPI twApi;
		ITwitterConfig twConfig;
		/// <summary>
		/// 
		/// </summary>
		/// <param name="account">the account to track</param>
		/// <param name="cook">Already logged in cookies</param>
		public StateUpdater(BO.Account account, ITwitterConfig twConfig)
		{
			this.Account = account;
			this.twConfig = twConfig;
			twApi = new TwitterAPI(twConfig);
		}

		/// <summary>
		/// Updates the stats of the Account
		/// </summary>
		/// <returns></returns>
		public bool UpdateAccountStats()
		{
			twConfig.AccessToken = Account.TwToken;
			twConfig.AccessTokenSecret = Account.TwSecret;

			var stat = Stats.GetTodaysAccountStat(Account.ID);
			stat.FbPageFanNum = GetFacebookFanCount(Account.FbPageID);
			stat.TwFollowersNum = GetTwitterFollowerCount(Account.TwUserID);

			stat.Update();

			Account.TwFollowersNum = stat.TwFollowersNum;
			Account.FbPageFanNum = stat.FbPageFanNum;
			Account.Update();

			return true;
		}

		/// <summary>
		/// Tracks the TarcesAccounts of the given Account and updates thier stats
		/// </summary>
		/// <returns></returns>
		public bool Track()
		{
			twConfig.AccessToken = Account.TwToken;
			twConfig.AccessTokenSecret = Account.TwSecret;

			foreach (var trace in Account.TracedAccounts)
			{
				UpdateTraceStat(trace);

			}
			return true;
		}

		

		private void UpdateTraceStat(BO.TracedAccount trace)
		{
			var stat = Logic.BusinessLogic.Stats.GetTodaysTraceStat(trace.ID);

			if (trace.AccountType == BO.TracedAccount.AccountTypes.Facebook)
				stat.FanNum = GetFacebookFanCount(trace.FbPageID);
			if (trace.AccountType == BO.TracedAccount.AccountTypes.Twitter)
				stat.FanNum = GetTwitterFollowerCount(trace.Name);

			stat.Update();

			trace.FanNum = stat.FanNum;
			trace.Update();
		}

		private int GetTwitterFollowerCount(long? id)
		{
			if (id.HasValue)
			{
				var res = twApi.GetUser(id.Value);
				if (res.ResultOption == Social.ServiceResultOptions.Success && res.Content.NumberOfFollowers.HasValue)
					return (int)res.Content.NumberOfFollowers.Value;
			}
			return 0;
		}

		private int GetTwitterFollowerCount(string name)
		{
			if (!string.IsNullOrEmpty(name))
			{
				var res = twApi.GetUser(name);
				if (res.ResultOption == Social.ServiceResultOptions.Success && res.Content.NumberOfFollowers.HasValue)
					return (int)res.Content.NumberOfFollowers.Value;
			}
			return 0;
		}

		private int GetFacebookFanCount(long? fbPageID)
		{
			if (!fbPageID.HasValue)
				return 0;

			var res = GraphApi.GetPageById(fbPageID.Value);
			if(res.ResultOption == Social.ServiceResultOptions.Success)
				return res.Content.likes;
			return 0;			
		}


	}
}
